<?php
	/**
	 * 接口文件
	 */
	 class InterfaceAction extends Action{
		/**
		 * 请求接口,发送手机短信验证码
		 * @param   $phone   给哪个手机发送验证码
		 * @return			 返回验证码
		 *
		 */
		public function getKey(){
			$phone = I('phone');
			$random = randomkeys(4);
			$post_data="action=send&userid=4127&account=877216091&password=147258369&mobile=".$phone."&content=".$random;
			$link="http://sms.chengweigg.com/sms.aspx";
			
			$ch = curl_init();
			curl_setopt($ch,CURLOPT_URL,$link);
			curl_setopt($ch,CURLOPT_POST,1); 
			curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			$result = curl_exec($ch);
			curl_close($ch);
			
			$arr['key'] = $result;
			
			$arr['keycode'] = $random;

			session($phone,$random);
			echo json_encode($arr);
		 }

		/**
		 * 注册用户
		 * @param  phone    传递的手机号
 		 * @param  key		传入验证码
		 * @param  password	传入密码
		 */
		public function register(){
			$data['regphone'] = I('regphone');
			$random = I('key');
			$data['password'] = md5(I('password'));
			if(session($phone)==$random){
				$model = M('vip');
				$result = $model ->data($data)->add();
				if($result){
					$arr['status'] = 1;					
				}
			}else{
				$arr['status'] = 0;	 //验证码错误
			}
			echo json_encode($arr);
		}

		/**
		 * 重置密码
		 * @param   phone		传递手机号
		 * @param   key			传递验证码
		 * @param	password    传入密码
		 * @return			返回正确还是错误
		 */
		public function upPassword(){
			$phone = I('phone');
			$key = I('key');
			if(session($phone)==$key){
				$data['status'] = 1;	 //验证码正确
				$data['password'] = md5(I('password'));
				$model = M('vips')->where("regphone=".$phone)->save($data);
			}else{
				$data['status'] = 0;	 //验证码不正确
			}
			echo json_encode($result);
		}
		

		/**
		 * 用户登录接口
		 * @param   vipname   用户输入的用户名
		 * @param   password  用户输入的密码
		 * @return			  返回信息
		 */
		
		public function login(){
			//接收用户输入的参数(用户名、密码)
			$data['vipname'] = I('vipname');
			$data['password'] = md5(I('password'));
			$model = M('vips');
			$result = $model->where($data)->find();
			
			if($result){
				$msg['status'] = 1;
				$arr = array('vip_id'=>$result['vip_id'],'vipname'=>$result['vipname']);
				Session("vips",$arr);
			}else{
				$msg['status'] = 0;
			}
			
			echo json_encode($msg);
		}

		/**
		 * 创建订单接口
		 * @param id			产品、服务id
		 * @param phone			联系方式
		 * @param carNum		车牌号码
		 * @param address		服务地址
		 * @param cleanTime		洗车时间
		 * @param isInner		是否清晰内饰
		 * @param vipname		vip用户名
		 * @return				返回0: 请先登录   1下单成功
		 */
		public function addOrder(){
			$data['device'] = I('device');			//设备类型
			$data['vip_id'] = I('vip_id');			//vip用户id
			$data['goods_id'] = I('goods_id');		//产品id
			$data['order_time'] = time();			//下单时间
			$data['order_sum'] = I('sum');			//订单总额
			$data['status'] = 0;					//已下订单
			$data['phone'] = I('phone');			//联系电话
			$data['car_num'] = I('car_num');		//车牌号码
			$data['carType'] = I('car_type');		//车型
			$data['color']  = I('color');			//颜色
			$data['address'] = I('address');		//服务地址
			$data['cleanTime'] = I('cleanTime');	//服务时间
			$data['iscleaninner'] = I('iscleaninner');//是否清晰内饰
			
			//var_dump(session('vips.'.I('vip_id')));
			
			if(!Session("?vips")){
				echo 0;
			}else{
				$order_id = M('Orders')->add($data);
				$result = M('cars')->data($data)->add();
				echo 1;
			}
		}
		
		/**
		 * 查询订单列表
		 * @param	 vip_id    vip用户id
		 * @return	 0表示请先登录
		 */
		public function getOrder(){
			$vip_id = I('vip_id');		//用户id	用户和订单：1对多				
			
			if(Session("?vips")){
				$model = D('Vips');
				$result = $model->relation(true)->find($vip_id);

				$model = M('goods');
				foreach($result['orders'] as $key=>$value){
					$data = $model ->where('goods_id='.$value['goods_id'])->getField('goods_id,goods_name,goods_thumb');
					$result['orders'][$key]['goods_name'] = $data[$value['goods_id']]['goods_name'];
					$result['orders'][$key]['goods_thumb'] = $data[$value['goods_id']]['goods_thumb'];
				}
				echo json_encode($result['orders']);
			}else{
				echo 0;	//请先登录
			}
		}
		/**
		 * 订单详情
		 * @param	  order_id   订单号
		 * @return				 返回订单详情   0：请先登录
		 */
		public function orderDetail(){
			$order_id = I('order_id');	
			if(Session("?vips")){
				$model = D('Orders');
				$result = $model -> relation(true)->find($order_id);
				echo json_encode($result);
			}else{
				echo 0;
			}
		}
		/**
		 * 评价订单
		 * @param	order_id	订单号
		 */
		public function evalOrder(){
			$vips = session('vips');
			$vip_id = $vips['vip_id'];
			$order_id = I('order_id');
			$data['scores'] = I('scores');
			$model = M('Employs');
			
			$emp_info = D('Orders')->field('emp_id')->relation(true)->find($order_id);
			$data['scores'] = (int)$emp_info['scores']+(int)I('scores');

			$result = $model->where('emp_id='.$emp_info['emp_id'])->save($data);
			if($result){
				$model->where('emp_id='.$emp_info['emp_id'])->setInc('score_nums');
				$scores = $model->field('score_nums,scores')->find($emp_info['emp_id']);
				$avg_score = ceil($scores['scores']/$scores['score_nums']);
				echo $avg_score.'%';
			}
		}
		/**
		 * 取消订单
		 * @param   订单
		 * @return	1取消成功   0请先登录
		 */
		public function cancleOrder(){
			$order_id = I('order_id');

			$vips = session('vips');
			$vip_id = $vips['vip_id'];
			if(Session("?vips")){
				$model = M('orders');
				$result = $model->where('order_id='.$order_id)->delete();
				echo 1;
			}else{
				echo 0;
			}			
		}

		/**
		 * 获得月白产品列表
		 * @param  
		 *
		 */
		public function goodsList(){
			$model = M('goods');
			$list = $model->select();
			echo json_encode($list);
		}
		/**
		 * 月白详细内容
		 * @param	goods_id	产品id
		 * @return	detail		详细信息
		 */
		public function goodsDetail(){
			$goods_id = I('goods_id');
			$detail = M('Goods')->find($goods_id);
			echo json_encode($detail);
		}
		/**
		 * 获得车况信息
		 * @param	car_num		车牌号
		 * @param	lng			经度
		 * @param	lat			纬度
		 * @param	start_date	日期  2014-12-2格式
		 * @return				指定月份洗车记录、洗车日期、排名
		 */
		public function CarDetail(){
			$car_num = I('car_num');
			
			//获得周围的人
			$lng = I('lng');
			$lat = I('lat');

			$squares = returnSquarePoint($lng, $lat);
			$info_sql = "select * from `yb_vips` where lat<>0 and lat>{$squares['right-bottom']['lat']} and lat<{$squares['left-top']['lat']} and lng>{$squares['left-top']['lng']} and lng<{$squares['right-bottom']['lng']} "; 
			
			$model = M('Vips');
			$addrs = $model->query($info_sql);
			
			$vips = session('vips');
			$vip_id = $vips['vip_id'];
			
			$vip_ids = '(';		//周围的用户
			foreach($addrs as $key=>$value){		
					$vip_ids .= $value['vip_id'].',';
				
			}
			$vip_ids = rtrim($vip_ids,',');
			$vip_ids .= ')';

			$start_date = I('start_date');
			$start_date = date('Y-m-01',strtotime($start_date));
			
			$last_date = date('Y-m-d', strtotime(date('Y-m-01', strtotime($start_date)) . ' +1 month -1 day'));
		
			$model = M('Cars');

			//查询该车辆洗车记录
			$result = $model -> where('cleantime between '.strtotime($start_date).' AND '.strtotime($last_date).' AND car_num='.$car_num)->select();

			
			//echo $model->getLastSql();die;
			$clean_num = count($result);	//查询某个月洗车次数
			$arr = array();
			foreach($result as $value){
				array_push($arr,date('Y-m-d',$value['cleantime']));
			}
			
			$result = $model -> where('cleantime between '.strtotime($start_date).' AND '.strtotime($last_date).' AND vip_id in '.$vip_ids)->order(count('vip_id'))->field(count('vip_id'))->group('vip_id')->select();
			//echo $model->getLastSql();die;
			
			$vips = session('vips');
			$vip_id = $vips['vip_id'];
			foreach($result as $key=>$value){
				if($value['vip_id']==$vip_id){
					$arr['rank'] = $key+1;
				}
			}			
			echo json_encode($arr);
		}
		/**
		 * 查询车辆空气指数
		 * @param	car_num		车牌号
		 * @return				返回每次洗车是的空气质量
		 */
		public function getPMI(){
			$car_num = I('car_num');
			$model = M('Cars');
			$car = array();

			//获得周围的人
			//获得周围的人
			$lng = I('lng');
			$lat = I('lat');

			$squares = returnSquarePoint($lng, $lat);
			$info_sql = "select * from `yb_vips` where lat<>0 and lat>{$squares['right-bottom']['lat']} and lat<{$squares['left-top']['lat']} and lng>{$squares['left-top']['lng']} and lng<{$squares['right-bottom']['lng']} ";
			
			$model = M('Cars');
			$result = $model->query($info_sql);

			$peoples = '(';		//周围的用户
			foreach($result as $key=>$value){		
					$peoples .= $value['vip_id'].',';
				
			}
			$peoples = rtrim($peoples,',');
			$peoples .= ')';
			
			$pmi_sql = "SELECT vip_id,avg(pmi) FROM yb_cars where vip_id in".$peoples .'group by vip_id';
			
			$pmi_result = $model->query($pmi_sql);
			
			$vips = session('vips');
			$vip_id = $vips['vip_id'];

			//pm排名
			foreach($pmi_result as $key=>$value){
				if($value['vip_id']==$vip_id){
					$pmi['rank'] = $key+1;
				}
			}
			$car[$car_num]['pmi_rank'] = $pmi['rank'];

			$jiaquan_sql = "SELECT vip_id,avg(jiaquan) FROM yb_cars where vip_id in".$peoples .' group by vip_id order by avg(jiaquan) asc';
			$jiaquan_result = $model->query($jiaquan_sql);
			
			//甲醛排名
			foreach($jiaquan_result as $key=>$value){
				if($value['vip_id']==$vip_id){
					$jiaquan['rank'] = $key+1;
				}
			}
			$car[$car_num]['jiaquan_rank'] = $jiaquan['rank'];

			//该车辆pmi记录
			$pmi_record = $model->where('car_num='.$car_num)->order('cleantime ASC')->select();
			
			//该车辆pmi平均值
			$pmi_avg = $model->where('car_num='.$car_num)->order('cleantime ASC')->avg('pmi');
			$car[$car_num]['pmi_avg'] = $pmi_avg;

			$pmis = array();
			foreach($pmi_record as $key => $value){ 
				$arr['cleantime'] = $value['cleantime'];	//洗车时间
				$arr['pmi'] = $value['pmi'];				//pmi指数
				array_push($pmis,$arr);	
			}
			$car[$car_num]['pmi_records'] = $pmis;
			//该车辆甲醛记录
			$jq_record = $model->where('car_num='.$car_num)->order('cleantime ASC')->select();
			$jiaquans = array();
			foreach($pmi_record as $key => $value){ 
				$arr['cleantime'] = $value['cleantime'];	//洗车时间
				$arr['jiaquan'] = $value['jiaquan'];				//pmi指数
				array_push($jiaquans,$arr);	
			}
			$car[$car_num]['jiaquan_records'] = $jiaquans;

			//该车辆甲醛平均值
			$jiaquan_avg = $model->where('car_num='.$car_num)->order('cleantime ASC')->avg('jiaquan');
			$car[$car_num]['jiaquan_avg'] = $jiaquan_avg;
			 
			 echo json_encode($car[$car_num]);
		}
		/**
		 * 根据日期查询胎压
		 * @param	date		传递的日期
		 * @param	car_num		车牌号码
		 * 
		 */
		public function getTire(){
			//胎压记录
			$date = strtotime(I('date'));
			$car_num = I('car_num');
			$model = M('Cars');
			$tire = $model->where('car_num='.$car_num.' AND cleantime='.$date)->order('cleantime ASC')->select();

			foreach($tire as $key => $value){ 
				$arr['cleantime'] = date('Y-m-d',$value['cleantime']);	//洗车时间
				$arr['left_top'] = $value['left_top'];				//pmi指数
				$arr['right_top'] = $value['right_top'];
				$arr['left_bottom'] = $value['left_bottom'];
				$arr['right_bottom'] = $value['right_bottom'];
			}
			echo json_encode($arr);
		}
		/**
		 * 
		 *
		 */
	 
	 }


?>